//
//  OrderDetailFooter.m
//  RRQS
//
//  Created by kyjun on 16/3/28.
//
//

#import "OrderDetailFooter.h"

@interface OrderDetailFooter ()<AMapSearchDelegate>
@property(nonatomic,strong) UIView* priceView;
@property(nonatomic,strong) UILabel* labelSumTitle;
@property(nonatomic,strong) UILabel* labelSumMoney;

@property(nonatomic,strong) UIView* orderView;
@property(nonatomic,strong) UILabel* labelOrderNO;
@property(nonatomic,strong) UILabel* labelPayWay;

@property(nonatomic,strong) UIView* actionView;
@property(nonatomic,strong) UIButton* btnStore;
@property(nonatomic,strong) UIButton* btnUser;
@property(nonatomic,strong) UIButton* btnStatus;

@property(nonatomic,strong) UIView* bottomView;
@property(nonatomic,strong) UIButton*  btnLocation;
@property(nonatomic,strong) UIImageView* icon;
@property(nonatomic,strong) UILabel* labelAddress;
@property(nonatomic,strong) UILabel* labelReGeocode;

@property (nonatomic, strong) AMapSearchAPI *search;

@end

@implementation OrderDetailFooter

-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if(self){
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self layoutUI];
        [self layoutConstraints];
    }
    return self;
}

#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    [self addSubview:self.priceView];
    [self.priceView addSubview:self.labelSumTitle];
    [self.priceView addSubview:self.labelSumMoney];
    [self addSubview:self.orderView];
    [self.orderView addSubview:self.labelOrderNO];
    [self.orderView addSubview:self.labelPayWay];
    [self addSubview:self.actionView];
    [self.actionView addSubview:self.btnStore];
    [self.actionView addSubview:self.btnUser];
    [self.actionView addSubview:self.btnStatus];
    [self addSubview:self.bottomView];
    [self.bottomView addSubview:self.btnLocation];
    [self.btnLocation addSubview:self.icon];
    [self.btnLocation addSubview:self.labelAddress];
    [self.bottomView addSubview:self.labelReGeocode];
    
    CALayer* border = [[CALayer alloc]init];
    border.frame = CGRectMake(0, 0, SCREEN_WIDTH, 1.0f);
    border.backgroundColor = [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f].CGColor;
    [self.contentView.layer addSublayer:border];
}

-(void)layoutConstraints{
    NSArray* formats = @[ @"H:|-0-[priceView]-0-|",@"H:|-0-[orderView]-0-|",@"H:|-0-[actionView]-0-|",@"H:|-0-[bottomView]-0-|",
                          @"V:|-0-[priceView(==priceHeight)]",@"V:[priceView][orderView(==priceHeight)][actionView(==actionHeight)][bottomView(>=bottomHeight)]", @"[bottomView]-0-|",
                          @"H:|-10-[labelSumTitle][labelSumMoney]-10-|", @"V:|-0-[labelSumTitle]-0-|", @"V:|-0-[labelSumMoney]-0-|",
                          @"H:|-10-[labelOrderNO][labelPayWay]-10-|", @"V:|-0-[labelOrderNO]-0-|", @"V:|-0-[labelPayWay]-0-|",
                          @"H:|-10-[btnStore]-10-[btnUser(btnStore)]-10-[btnStatus(btnStore)]-10-|",@"V:|-10-[btnStore]-10-|", @"V:|-10-[btnUser]-10-|",@"V:|-10-[btnStatus]-10-|",
                          @"H:|-0-[btnLocation]-0-|",@"V:|-10-[btnLocation(==btnLocationHeighh)]",
                          @"H:|-10-[icon(==iconSize)][labelAddress]-10-|", @"V:|-15-[icon(==iconSize)]", @"V:|-0-[labelAddress]-0-|",
                          @"H:|-10-[labelReGeocode]-10-|",@"V:[btnLocation][labelReGeocode(>=0)]-0-|"
                          ];
    NSDictionary* metrics = @{ @"priceHeight":@(40), @"actionHeight":@(60), @"bottomHeight":@(60), @"actionBtnHeight":@(40), @"btnLocationHeighh":@(50), @"iconSize":@(20)};
    NSDictionary* views = @{ @"priceView":self.priceView, @"orderView":self.orderView, @"actionView":self.actionView, @"bottomView":self.bottomView,
                              @"labelSumTitle":self.labelSumTitle, @"labelSumMoney":self.labelSumMoney, @"labelOrderNO":self.labelOrderNO, @"labelPayWay":self.labelPayWay,
                              @"btnStore":self.btnStore, @"btnUser":self.btnUser, @"btnStatus":self.btnStatus, @"btnLocation":self.btnLocation, @"icon":self.icon, @"labelAddress":self.labelAddress,
                              @"labelReGeocode":self.labelReGeocode
                             };
    for (NSString* format in formats) {
        NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views];
        [self addConstraints:constraints];
    }
}

#pragma mark =====================================================  SEL
-(IBAction)connectionTouch:(UIButton*)sender{
    EnumOrderAction action;
    NSString* phone;
    if(self.btnStore == sender){
        action = connectionBusiness;
        phone = self.entity.storePhone;
    }else if (self.btnUser == sender ){
        action = connectionClient;
        phone = self.entity.userPhone;
    }else{
        action = confirmTake;
    }
    if(self.btnStatus == sender){
        if(self.delegate && [self.delegate respondsToSelector:@selector(orderDetailTake:)]){
            [self.delegate orderDetailTake:self.entity];
        }
    }else{
        if(self.delegate && [self.delegate respondsToSelector:@selector(orderDetailEnum:phone:)]){
            [self.delegate orderDetailEnum:action phone:phone];
        }
    }
}
-(IBAction)btnLocationTouch:(id)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(locationMapLocation:)]){
        [self.delegate locationMapLocation:self.entity];
    }
}
/* 逆地理编码回调. */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if (response.regeocode != nil)
    {
        self.labelReGeocode.text =  response.regeocode.formattedAddress;
    }
}
#pragma mark =====================================================  property package
-(void)setEntity:(MStore *)entity{
    if(entity){
        _entity = entity;
        self.labelSumMoney.text = entity.sumMoney;
        self.labelOrderNO.text = [NSString stringWithFormat:@"订单号 : %@",entity.subOrderID];
        self.labelPayWay.text = entity.payType;
        if( entity.status == expressStatusFinished){
            self.btnStatus.backgroundColor = [UIColor colorWithRed:167/255.f green:167/255.f blue:167/255.f alpha:1.0];
            self.btnStatus.userInteractionEnabled = NO;
            [self.btnStatus setTitle:@"已完成" forState:UIControlStateNormal];
        }else if (entity.status == expressStatusIng){
            if(self.isWaitTask) {
                self.btnStatus.backgroundColor = [UIColor colorWithRed:167/255.f green:167/255.f blue:167/255.f alpha:1.0];
                self.btnStatus.userInteractionEnabled = NO;
                [self.btnStatus setTitle:@"已取货" forState:UIControlStateNormal];
            }else{
                self.btnStatus.userInteractionEnabled = NO;
                self.btnStatus.backgroundColor = [UIColor colorWithRed:167/255.f green:167/255.f blue:167/255.f alpha:1.0];
                [self.btnStatus setTitle:@"确认送达" forState:UIControlStateNormal];
            }
        }else if (entity.status == expressStatusCanceling || entity.status == expressStatusCancelled){
            self.btnStatus.backgroundColor = [UIColor colorWithRed:167/255.f green:167/255.f blue:167/255.f alpha:1.0];
            self.btnStatus.userInteractionEnabled = NO;
            [self.btnStatus setTitle:@"已取消" forState:UIControlStateNormal];
        }else if(entity.status == expressStatusWating){
            self.btnStatus.backgroundColor = [UIColor colorWithRed:167/255.f green:167/255.f blue:167/255.f alpha:1.0];
            self.btnStatus.userInteractionEnabled = YES;
            [self.btnStatus setTitle:@"确认取货" forState:UIControlStateNormal];
            [self.btnStatus setBackgroundColor:[UIColor colorWithRed:76/255.f green:182/255.f blue:226/255.f alpha:1.0]];
        }
        if(!self.isWaitTask){ //配送中 反查 收货人地址
            self.labelAddress.text = self.address;
            if([WMHelper isEmptyOrNULLOrnil:self.addressRoad]){
            if([WMHelper isEmptyOrNULLOrnil:self.entity.phoneAddress]){
                self.labelReGeocode.text =  @"";
            }else{
                NSArray* empty = [self.entity.phoneAddress componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
                if(empty.count == 2){
                    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
                    regeo.location = [AMapGeoPoint locationWithLatitude:[[empty lastObject] doubleValue] longitude:[[empty firstObject] doubleValue]];
                    regeo.requireExtension = YES;
                    regeo.radius = 1000;
                    [self.search AMapReGoecodeSearch:regeo];
                }else{
                    self.labelReGeocode.text =  @"";
                }
            }
            }else{
                self.labelReGeocode.text = self.addressRoad;
            }
        }else{ //待取货 反查店铺地址
              self.labelAddress.text = self.entity.storeAddress;
            if([WMHelper isEmptyOrNULLOrnil:self.entity.lat] || [WMHelper isEmptyOrNULLOrnil:self.entity.lng]){
                self.labelReGeocode.text =  @"";
            }else{
                AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
                regeo.location = [AMapGeoPoint locationWithLatitude:[self.entity.lat doubleValue] longitude:[self.entity.lng doubleValue]];
                regeo.requireExtension = YES;
                regeo.radius = 1000;
                [self.search AMapReGoecodeSearch:regeo];
            }
        }
    }
}

-(void)setAddress:(NSString *)address{
    if(address){
        _address = address;
        self.labelAddress.text = address;
    }else{
        self.labelAddress.text =  @"";
    }
}

-(UIView *)priceView{
    if(!_priceView){
        _priceView = [[UIView alloc]init];
        CALayer* border = [[CALayer alloc]init];
        border.frame = CGRectMake(0, 39.f, SCREEN_WIDTH, 1.0f);
        border.backgroundColor = [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f].CGColor;
        [_priceView.layer addSublayer:border];
        _priceView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _priceView;
}

-(UILabel *)labelSumTitle{
    if(!_labelSumTitle){
        _labelSumTitle = [[UILabel alloc]init];
        _labelSumTitle.textColor = [UIColor colorWithRed:152/255.f green:152/255.f blue:152/255.f alpha:1.0];
        _labelSumTitle.text = @"总计";
        _labelSumTitle.textAlignment = NSTextAlignmentLeft;
        _labelSumTitle.font = [UIFont systemFontOfSize:14.f];
        _labelSumTitle.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _labelSumTitle;
}

-(UILabel *)labelSumMoney{
    if(!_labelSumMoney){
        _labelSumMoney = [[UILabel alloc]init];
        _labelSumMoney.textColor = [UIColor colorWithRed:152/255.f green:152/255.f blue:152/255.f alpha:1.0];
        _labelSumMoney.textAlignment = NSTextAlignmentRight;
        _labelSumMoney.font = [UIFont systemFontOfSize:14.f];
        _labelSumMoney.translatesAutoresizingMaskIntoConstraints  = NO;
        _labelSumMoney.text = @"100.1";
    }
    return _labelSumMoney;
}

-(UIView *)orderView{
    if(!_orderView){
        _orderView = [[UIView alloc]init];
        CALayer* border = [[CALayer alloc]init];
        border.frame = CGRectMake(0, 39.f, SCREEN_WIDTH, 1.0f);
        border.backgroundColor = [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f].CGColor;
        [_orderView.layer addSublayer:border];
        _orderView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _orderView;
}

-(UILabel *)labelOrderNO{
    if(!_labelOrderNO){
        _labelOrderNO = [[UILabel alloc]init];
        _labelOrderNO.textColor = [UIColor colorWithRed:152/255.f green:152/255.f blue:152/255.f alpha:1.0];
        _labelOrderNO.textAlignment = NSTextAlignmentLeft;
        _labelOrderNO.font = [UIFont systemFontOfSize:14.f];
        _labelOrderNO.translatesAutoresizingMaskIntoConstraints = NO;
        _labelOrderNO.text = @"a你说地方";
    }
    return _labelOrderNO;
}

-(UILabel *)labelPayWay{
    if(!_labelPayWay){
        _labelPayWay = [[UILabel alloc]init];
        _labelPayWay.textColor = [UIColor colorWithRed:152/255.f green:152/255.f blue:152/255.f alpha:1.0];
        _labelPayWay.textAlignment = NSTextAlignmentRight;
        _labelPayWay.font =[UIFont systemFontOfSize:14.f];
        _labelPayWay.translatesAutoresizingMaskIntoConstraints = NO;
        _labelPayWay.text = @"1343dsfs";
    }
    return _labelPayWay;
}

-(UIView *)actionView{
    if(!_actionView){
        _actionView = [[UIView alloc]init];
        _actionView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _actionView;
}

-(UIButton *)btnStore
{
    if(!_btnStore){
        _btnStore = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnStore.layer.masksToBounds = YES;
        _btnStore.layer.cornerRadius = 5.f;
        _btnStore.layer.borderColor = [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f].CGColor;
        _btnStore.layer.borderWidth = 1.f;
        [_btnStore setTitle:@"联系商家" forState:UIControlStateNormal];
        [_btnStore setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_btnStore setBackgroundColor:[UIColor whiteColor]];
        _btnStore.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_btnStore addTarget:self action:@selector(connectionTouch:) forControlEvents:UIControlEventTouchUpInside];
        _btnStore.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _btnStore;
}

-(UIButton *)btnUser{
    if(!_btnUser){
        _btnUser = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnUser.layer.masksToBounds = YES;
        _btnUser.layer.cornerRadius = 5.f;
        _btnUser.layer.borderColor = [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f].CGColor;
        _btnUser.layer.borderWidth = 1.f;
        [_btnUser setTitle:@"联系顾客" forState:UIControlStateNormal];
        [_btnUser setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnUser setBackgroundColor:[UIColor colorWithRed:24/255.f green:180/255.f blue:152/255.f alpha:1.0]];
        _btnUser.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_btnUser addTarget:self action:@selector(connectionTouch:) forControlEvents:UIControlEventTouchUpInside];
        _btnUser.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _btnUser;
}

-(UIButton *)btnStatus{
    if(!_btnStatus){
        _btnStatus = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnStatus.layer.masksToBounds = YES;
        _btnStatus.layer.cornerRadius = 5.f;
        _btnStatus.layer.borderColor = [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f].CGColor;
        _btnStatus.layer.borderWidth = 1.f;
        [_btnStatus setTitle:@"确认取货" forState:UIControlStateNormal];
        [_btnStatus setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnStatus setBackgroundColor:[UIColor colorWithRed:76/255.f green:182/255.f blue:226/255.f alpha:1.0]];
        _btnStatus.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_btnStatus addTarget:self action:@selector(connectionTouch:) forControlEvents:UIControlEventTouchUpInside];
        _btnStatus.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _btnStatus;
}

-(UIView *)bottomView{
    if(!_bottomView){
        _bottomView = [[UIView alloc]init];
        _bottomView.backgroundColor = [UIColor whiteColor];
        CALayer* border = [[CALayer alloc]init];
        border.frame = CGRectMake(0, 0.f, SCREEN_WIDTH, 10.0f);
        border.backgroundColor = [UIColor colorWithRed:241/255.f green:241/255.f blue:241/255.f alpha:1.0].CGColor;
        [_bottomView.layer addSublayer:border];
        _bottomView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _bottomView;
}

-(UIButton *)btnLocation{
    if(!_btnLocation){
        _btnLocation = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnLocation.backgroundColor = [UIColor whiteColor];
        [_btnLocation addTarget:self action:@selector(btnLocationTouch:) forControlEvents:UIControlEventTouchUpInside];
        _btnLocation.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _btnLocation;
}

-(UIImageView *)icon{
    if(!_icon){
        _icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed: @"locate"]];
        _icon.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _icon;
}

-(UILabel *)labelAddress{
    if(!_labelAddress){
        _labelAddress = [[UILabel alloc]init];
        _labelAddress.font = [UIFont systemFontOfSize:12.f];
        _labelAddress.numberOfLines = 0;
        _labelAddress.translatesAutoresizingMaskIntoConstraints = NO;
        _labelAddress.text = @"新民市东环街41号";
    }
    return _labelAddress;
}

-(UILabel *)labelReGeocode{
    if(!_labelReGeocode){
        _labelReGeocode = [[UILabel alloc]init];
        _labelReGeocode.textColor = [UIColor grayColor];
        _labelReGeocode.font = [UIFont systemFontOfSize:12.f];
        _labelReGeocode.lineBreakMode = NSLineBreakByCharWrapping;
        _labelReGeocode.numberOfLines = 0;
        _labelReGeocode.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _labelReGeocode;
}

-(AMapSearchAPI *)search{
    if(!_search){
        _search = [[AMapSearchAPI alloc] init];
        _search.delegate = self;
    }
    return _search;
}
@end
